package ru.vsu.cs.pm.pages;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.AccountManager;
import ru.vsu.cs.pm.models.Account;
import ru.vsu.cs.pm.models.AccountModel;
import ru.vsu.cs.pm.models.User;

import java.util.Iterator;

/**
 * User: Oleg
 * Date: 23.12.2010
 * Time: 3:13:46
 */
@MountPath(path = "accounts")
public class AccountListPage extends BasePage {
    @SpringBean
    private AccountManager accountManager;

    private static final int ROWS_PER_PAGE = 25;

    public AccountListPage() {
        IColumn[] columns = new IColumn[] {
            new PropertyColumn<Account>(Model.of("Title"), "title"),
            new PropertyColumn<Account>(Model.of("Description"), "description"),
            new PropertyColumn<Account>(Model.of("Money"), "money"),
                new PropertyColumn<Account>(Model.of("Type"), "type"),
            new AbstractColumn<Account>(Model.of("Actions")) {
                @Override
                public void populateItem(Item<ICellPopulator<Account>> cell, String wicketId, IModel<Account> model) {
                    cell.add(new ActionPanel(wicketId, model));
                }
            }
        };

        User user = PMSession.get().getAuthenticatedUser();
        SortableDataProvider<Account> dataProvider = new AccountsDataProvider(user);
        DefaultDataTable accountsTable = new DefaultDataTable("accountsTable", columns, dataProvider, ROWS_PER_PAGE);
        add(accountsTable);

        add(new Link("addAccountLink") {
            @Override
            public void onClick() {
                setRedirect(true);
                setResponsePage(AccountPage.class);
            }
        });
    }

    private class AccountsDataProvider extends SortableDataProvider<Account> {

        private User user;

        public AccountsDataProvider(User user) {
            this.user = user;
        }
        @Override
        public Iterator<? extends Account> iterator(int start, int count) {
            return accountManager.getAccountsForUser(user, start, count).iterator();
        }

        @Override
        public int size() {
            return accountManager.getNumberOfUserAccounts(user);
        }

        @Override
        public IModel<Account> model(Account account) {
            return new AccountModel(account);
        }
    }

    private class ActionPanel extends Panel {

        public ActionPanel(String wicketId, final IModel<Account> account) {
            super(wicketId);

            add(new Link("editAction") {
                @Override
                public void onClick() {
                    setRedirect(true);
                    setResponsePage(new AccountPage(account));
                }
            });

            add(new Link("removeAction") {
                @Override
                public void onClick() {
                    // TODO implement
                }
            });
        }
    }
}
